<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['banner:banner:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['banner:banner:remove']"
          >删除</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="bannerList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id编号" align="center" prop="id" /> -->
      <el-table-column
        label="轮播图"
        align="center"
        prop="picUrl"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.picUrl" width="fit-content" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="启用状态" align="center" prop="enabled">
        <template slot-scope="scope">
          <el-switch 
            v-model="scope.row.enabled"
            @change="(value) =>changeStatus(value, scope.$index, scope.row)"
            :active-value="1" 
            :inactive-value="0"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          {{ timestampToDate(scope.row.createTime)}}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, scope.$index)"
            v-hasPermi="['banner:banner:edit']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['banner:banner:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <add :open="open" :title="title" @close="refreshPage"></add>
   
    <edit :open="visible"
      :row="row"
     :title="title" @close="editCallback"
     @dialogClose="dialogClose"
     ></edit>
   
  </div>
</template>

<script>
import {
  listBanner,
  getBanner,
  delBanner,
  addBanner,
  updateBanner
} from '@/api/banner/banner'
import { OPEN_OR_CLOSE } from '@/constant/const'
import { timestampToDate } from '@/utils/time'

export default {
  name: 'Banner',
  data() {
    return {
      openOrClose:OPEN_OR_CLOSE,
      // 遮罩层
      loading: true,
      selectIndex: 0,
      // 选中数组
      ids: [],
      visible: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      row: {},
      // 首页轮播图表格数据
      bannerList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enabled: null
      },
      // 表单参数
      form: {},
     
    }
  },
  created() {
    this.getList()
  },

  components: {
    add: () => import('./add.vue'),
    edit: () => import('./edit.vue')
  },

  methods: {
    timestampToDate,
    /** 查询首页轮播图列表 */
    getList() {
      this.loading = true
      listBanner(this.queryParams).then((response) => {
        this.bannerList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    refreshPage(visible) {
      this.getList();
      this.open = visible;
    },

    editCallback(form, visible) {
      console.log("form", form);
      this.bannerList[this.selectIndex].enabled = form.enabled;
      this.bannerList[this.selectIndex].picUrl = form.picUrl;
      this.visible = visible
    },
    changeStatus(value, index, row) {
      updateBanner({
        id: row.id,
        enabled: value
      }).then(res => {
        this.$message.success('修改成功')

        this.bannerList[index].enabled = value;
      })

    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },

    dialogClose(visible) {
      this.visible = visible;
    },
   
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加首页轮播图'
    },
    /** 修改按钮操作 */
    handleUpdate(row, index) {
      
      this.selectIndex = index;

      this.visible = true;

      this.row = row;

      console.log("this.row", this.row, this.selectIndex);

    },
   
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除首页轮播图编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBanner(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'banner/banner/export',
        {
          ...this.queryParams,
          ids: this.ids.length? this.ids : undefined
        },
        `banner_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
